/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.jpdftools.pdfmerger.thread;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.logging.Level;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import project.jpdfexport.tools.ExPDFMergerManager;
import project.jpdfexport.tools.MergeFile;
import project.jpdftools.animation.*;
import project.jpdftools.constants.Const;
import project.jpdftools.managers.LocalizationManager;
import project.jpdftools.managers.LoggerManager;
import project.jpdftools.other.JOpenFile;
import project.jpdftools.other.KeyValue;

/**
 *
 * @author Mido
 */
public class PDFMergerThread extends AnimationWorkerThread {

    private JTable table;
    private String title;
    private String author;
    private String subject;
    private String keyWords;
    private File selectedFile;

    public PDFMergerThread(AnimationWorker animationWorker, JTable table, String title, String author, String subject, String keyWords, File selectedFile) {
        super(animationWorker);
        this.table = table;
        this.title = title;
        this.author = author;
        this.subject = subject;
        this.keyWords = keyWords;
        this.selectedFile = selectedFile;
    }

    @Override
    public void run() {
        boolean result;
        try {
            // start the animation
            this.getAnimationWorker().execute();

            int list_size = table.getRowCount();
            MergeFile[] list = new MergeFile[list_size];
            // get data from source pdfs
            for (int i = 0; i < list_size; i++) {
                list[i] = new MergeFile(new FileInputStream(new File(((KeyValue) table.getValueAt(i, 0)).getKey())),
                        Integer.valueOf(table.getValueAt(i, 2).toString()), Integer.valueOf(table.getValueAt(i, 3).toString()));
            }

            // pass the parameters to the merge class
            ExPDFMergerManager merge_files = new ExPDFMergerManager(new FileOutputStream(selectedFile + ".pdf"), list);
            result = merge_files.mergeWithBounds(Const.program_version, title, author, subject, keyWords);
        } catch (Exception ex) {
            result = false;
        }
        // kill the animation thread
        this.getAnimationWorker().setKillSignal(true);

        // set operation result and finalize
        // must be executed in the EventDispatchThread
        final boolean operationResult = result;
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                if (operationResult == true) {
                    Object[] options = {LocalizationManager.getCurrent_resource().getString("OPEN_FILE"),
                        LocalizationManager.getCurrent_resource().getString("DONT_OPEN_FILE")};

                    // ask the user if he/she wants to open the merged file
                    JOpenFile.showOptionDialog(LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_SUCCESS"),
                            LocalizationManager.getCurrent_resource().getString("SUCCESS"), options, selectedFile + ".pdf");
                    LoggerManager.getLogger().log(Level.INFO, "Files merged successfully to: {0}", selectedFile + ".pdf");
                } else {
                    // show error message
                    JOptionPane.showMessageDialog(null, LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_FAIL"),
                            LocalizationManager.getCurrent_resource().getString("ERROR"), 0);
                    LoggerManager.getLogger().log(Level.SEVERE, "Merging files failed.");
                }
            }
        });
    }
}
